package com.zdb.demo.java.flink;

import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.AssignerWithPeriodicWatermarks;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.streaming.api.functions.timestamps.AscendingTimestampExtractor;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class HotItems {
    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration(){{
            setInteger("rest.port", 9191);
            setBoolean("local.start-webserver", true);
        }};
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);
        env.setParallelism(4);

        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
        DataStreamSource<String> dstream = env.readTextFile("/Users/zhudebin/Documents/iworkspace/flink-demo/hotItemsAnalysis/src/main/resources/UserBehavior.csv");

        dstream.map(line -> {
            String[] strs = line.split(",");
            return new UserBehavior(
                    Long.parseLong(strs[0].trim()),
                    Long.parseLong(strs[1].trim()),
                    Integer.parseInt(strs[2].trim()),
                    StringUtils.trim(strs[3].trim()),
                    Long.parseLong(strs[4].trim())
                    );
        })
                .assignTimestampsAndWatermarks(new AscendingTimestampExtractor<UserBehavior>() {
                    @Override public long extractAscendingTimestamp(UserBehavior element) {
                        return element.timestamp * 1000;
                    }
                })
                .filter(v -> "pv".equals(v.behavior))
                .keyBy(v -> v.itemId)
                .timeWindow(Time.minutes(60), Time.minutes(5))
                .aggregate(new AggregateFunction<UserBehavior, Long, Long>() {
                               @Override public Long createAccumulator() {
                                   return 0L;
                               }

                               @Override public Long add(UserBehavior value, Long accumulator) {
                                   return accumulator + 1;
                               }

                               @Override public Long getResult(Long accumulator) {
                                   return accumulator;
                               }

                               @Override public Long merge(Long a, Long b) {
                                   return a + b;
                               }
                           }, new WindowFunction<Long, ItemViewCount, Long, TimeWindow>() {
                               @Override
                               public void apply(Long key, TimeWindow window, Iterable<Long> input,
                                       Collector<ItemViewCount> out) throws Exception {
                                   Iterator<Long> iter = input.iterator();
                                   if(iter.hasNext()) {
                                       out.collect(new ItemViewCount(key, window.getEnd(), iter.next()));
                                       while (iter.hasNext()) {
                                           System.out.println("------- window function Iter has next:" + iter.next());
                                       }
                                   }
                               }
                           }
                )
                .keyBy(v -> v.windowEnd)
                .process(new KeyedProcessFunction<Long, ItemViewCount, String>() {
                    // 取前3名
                    int size = 3;
                    ListState<ItemViewCount> listState = null;

                    @Override public void open(Configuration parameters) throws Exception {
                        super.open(parameters);
                        ListStateDescriptor itemStateDesc = new ListStateDescriptor<ItemViewCount>("cnt", ItemViewCount.class);
                        listState = getRuntimeContext().getListState(itemStateDesc);
                    }

                    @Override
                    public void processElement(ItemViewCount value, Context ctx,
                            Collector<String> out) throws Exception {
                        listState.add(value);

                        ctx.timerService().registerEventTimeTimer(value.windowEnd + 100);
                    }

                    @Override
                    public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out)
                            throws Exception {
                        Iterable<ItemViewCount> iter = listState.get();
                        List<ItemViewCount> list = new ArrayList<>();
                        iter.forEach(itemViewCount -> {
                            list.add(itemViewCount);
                        }
                        );
                        list.sort(new Comparator<ItemViewCount>() {
                            @Override public int compare(ItemViewCount o1, ItemViewCount o2) {
                                if(o1.count.longValue() > o2.count) {
                                    return -1;
                                } else if(o1.count.longValue() == o2.count.longValue()) {
                                    return 0;
                                } else {
                                    return 1;
                                }
                            }
                        });
                        StringBuffer sb = new StringBuffer();
                        sb.append("------- 排名 ------");
                        for(int i=0; i<3; i++) {
                            sb.append("NO:" + (i+1) + "," + list.get(i) + "\n");
                        }
                        out.collect(sb.toString());
                    }
                })

                .print("----cnt--");

        env.execute("hot items");

    }
}

class UserBehavior {
    Long userId;
    Long itemId;
    Integer categoryId;
    String behavior;
    Long timestamp;

    public UserBehavior() {
    }

    public UserBehavior(Long userId, Long itemId, Integer categoryId, String behavior,
            Long timestamp) {
        this.userId = userId;
        this.itemId = itemId;
        this.categoryId = categoryId;
        this.behavior = behavior;
        this.timestamp = timestamp;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getItemId() {
        return itemId;
    }

    public void setItemId(Long itemId) {
        this.itemId = itemId;
    }

    public Integer getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }

    public String getBehavior() {
        return behavior;
    }

    public void setBehavior(String behavior) {
        this.behavior = behavior;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    @Override public String toString() {
        return "UserBehavior{" +
                "userId=" + userId +
                ", itemId=" + itemId +
                ", categoryId=" + categoryId +
                ", behavior='" + behavior + '\'' +
                ", timestamp=" + timestamp +
                '}';
    }
}

// 中间聚合结果样例类
class ItemViewCount {
    Long itemId;
    Long windowEnd;
    Long count;

    public ItemViewCount() {
    }

    public ItemViewCount(Long itemId, Long windowEnd, Long count) {
        this.itemId = itemId;
        this.windowEnd = windowEnd;
        this.count = count;
    }

    public Long getItemId() {
        return itemId;
    }

    public void setItemId(Long itemId) {
        this.itemId = itemId;
    }

    public Long getWindowEnd() {
        return windowEnd;
    }

    public void setWindowEnd(Long windowEnd) {
        this.windowEnd = windowEnd;
    }

    public Long getCount() {
        return count;
    }

    public void setCount(Long count) {
        this.count = count;
    }

    @Override public String toString() {
        return "ItemViewCount{" +
                "itemId=" + itemId +
                ", windowEnd=" + windowEnd +
                ", count=" + count +
                '}';
    }
}

